package com.woniu.dao.impl;

import com.woniu.dao.StudentDao;
import com.woniu.entity.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {

    @Override
    public void insertStudent(Student student) {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\aaa\\students.txt", true));){
            bw.write(student.getId()+"-"+student.getName()+"-"+student.getAge()+"-"+student.getGender()+"-"+student.getPhone());
            bw.newLine();
            bw.flush();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public Student selectStudentName(String name) {
        Student student = null;
        try (BufferedReader br = new BufferedReader(new FileReader("d:\\aaa\\students.txt"));){
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] str = line.split("-");
                if(str[1].equals(name)){
                    student = new Student();
                    student.setId(str[0]);
                    student.setName(str[1]);
                    student.setAge(str[2]);
                    student.setGender(str[3]);
                    student.setPhone(str[4]);
                    return student;
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return student;
    }

    @Override
    public Student selectStudentPhone(String phone) {
        Student student = null;
        try (BufferedReader br = new BufferedReader(new FileReader("d:\\aaa\\students.txt"));){
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] str = line.split("-");
                if(str[4].equals(phone)){
                    student = new Student();
                    student.setId(str[0]);
                    student.setName(str[1]);
                    student.setAge(str[2]);
                    student.setGender(str[3]);
                    student.setPhone(str[4]);
                    return student;
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return student;
    }

    @Override
    public List<Student> selectStudentAll() {
        List<Student> list = new ArrayList<>();
      try(BufferedReader br = new BufferedReader(new FileReader("d:\\aaa\\students.txt"))){
          String line = null;
          while ((line = br.readLine()) != null) {
              String[] str = line.split("-");
              Student student = new Student();
              student.setId(str[0]);
              student.setName(str[1]);
              student.setAge(str[2]);
              student.setGender(str[3]);
              student.setPhone(str[4]);
              list.add(student);
          }
      }catch (Exception e){
          e.printStackTrace();
      }
        return list;
    }

    @Override
    public File ClearStudent() {
        File file = new File("d:\\aaa\\students.txt");
        try (BufferedWriter br = new BufferedWriter(new FileWriter(file))) {
            br.write(""); // 清空文件内容
        } catch (IOException e) {
            // 更优雅地处理异常，例如打印堆栈跟踪或记录日志
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return file;
    }
}
